package com.hui.qiniucloud.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hui.qiniucloud.entity.AICharacter;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import java.util.List;

@Mapper
public interface CharacterMapper extends BaseMapper<AICharacter> {

    @Select("SELECT * FROM `character` WHERE deleted = 0 AND (name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%'))")
    List<AICharacter> searchByKeyword(String keyword);

    /**
     * 根据用户ID查询角色
     */
    @Select("SELECT * FROM `character` WHERE deleted = 0 AND user_id = #{userId} ORDER BY created_time DESC")
    List<AICharacter> selectByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和关键词搜索角色
     */
    @Select("SELECT * FROM `character` WHERE deleted = 0 AND user_id = #{userId} AND (name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) ORDER BY created_time DESC")
    List<AICharacter> searchByUserIdAndKeyword(@Param("userId") Long userId, @Param("keyword") String keyword);

    /**
     * 查询系统预设角色（无用户关联）
     */
    @Select("SELECT * FROM `character` WHERE deleted = 0 AND user_id IS NULL ORDER BY created_time ASC")
    List<AICharacter> selectSystemCharacters();

    default List<AICharacter> selectAllActive() {
        QueryWrapper<AICharacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByAsc("id");
        return this.selectList(queryWrapper);
    }

    default AICharacter selectActiveById(String id) {
        QueryWrapper<AICharacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("deleted", 0);
        return this.selectOne(queryWrapper);
    }
}